<form action="#" className="layui-form">
    <div className="layui-form-item">
        <div className="layui-inline">
            <label>名称:</label>
            <div className="layui-inline">
                <input type="text" name="user" autoComplete="off" placeholder="请输入名称"
                       className="layui-input">
            </div>
            状态:
            <div className="layui-inline">
                <select name="msg">
                    <option value="-100">请选择</option>
                    <option value="0">无效</option>
                    <option value="1">success</option>
                </select>
            </div>
            <button className="layui-btn" lay-submit lay-filter="category-table">搜索</button>
        </div>
    </div>
</form>
<script type="text/html" id="category-head-bar">
    <div className="layui-btn-container">
        <button className="layui-btn layui-btn-sm" lay-event="goEdit"><i className="layui-icon">&#xe654;</i>添加</button>
        <button className="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i className="layui-icon">&#xe640;</i>删除
        </button>
    </div>
</script>
<script type="text/html" id="category-category-bar">
    <a className="layui-btn layui-btn-xs" lay-event="edit-category">编辑</a>
</script>
<table className="layui-table" lay-filter="category-table" id="category-table">
</table>
<script type="text/javascript">
    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        form.render();
        table.render({
            id: "category-table",
            elem: '#category-table'
            , toolbar: '#category-head-bar'
            , cellMinWidth: 80
            , url: '/logs/logs' //数据接口
            , page: true //开启分页
            , response: {
                statusCode: 1 //重新规定成功的状态码为1，table 组件默认为 0
            }
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.result ? res.result.total : 0, //解析数据长度
                    "data": res.result ? res.result.list : null //解析数据列表
                };
            },
            done: function (res, curr, count) {
                if (res.code == 401) {
                    location.href = "/login.html";
                }
            }
            , cols: [[
                {checkbox: true},
                {field: 'id', title: 'ID', sort: true}
                , {field: 'user', title: '用户名称'}
                , {field: 'params', title: '参数'}
                , {field: 'caozuo', title: '用户操作'}
                , {field: 'date', title: '时间'}
                , {field: 'msg', title: '状态'}
                , {fixed: 'right', title: '操作', toolbar: '#category-category-bar', width: 180}
            ]]
        });
        //头工具栏事件
        table.on('toolbar(category-table)', function (obj) {
            switch (obj.event) {
                case 'goEdit':
                    openEditWindow(null);
                    break;
                case 'delete':
                    var data = table.checkStatus('category-table').data;
                    if (data.length > 0) {
                        layer.confirm('真的删除行么', function (index) {
                            var param = ""
                            $.each(data, function (i, obj) {
                                if (i == 0) {
                                    param += "ids=" + obj.id;
                                }
                                if (i != 0) {
                                    param += "&ids=" + obj.id;
                                }
                            })
                            $.ajax({
                                url: '/logs/logs?' + param,
                                method: 'delete',
                                success: function (result) {
                                    if (result.code == 401) {
                                        location.href = "/login.html";
                                        return;
                                    }
                                    if (result.code == 1) {
                                        table.reload('category-table', {});
                                    } else {
                                        alert(result.msg)
                                    }
                                    layer.close(index);
                                }
                            })
                        });
                    }
                    break;
            }
            ;
            ;
        });
        //监听行工具事件
        table.on('tool(category-table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit-category': {
                    openEditWindow(data);
                    break;
                }

            }
        });

        function openEditWindow(data) {
            layer.open({
                type: 1,
                content: data == null ? $('#category-add-layer').html() : $('#category-edit-layer').html(),
                title: '编辑'
                , area: ['500px', '400px'],
                btn: ['提交', '取消'] //可以无限个按钮
                , yes: function (index, layero) {
                    layer.load()
                    $.ajax({
                        url: "/logs/logs",
                        contentType: "application/json;charset=UTF-8",
                        data: data == null ? JSON.stringify($("#category-add-form").serializeJson()) : JSON.stringify($("#category-edit-form").serializeJson()),
                        method: data == null ? 'POST' : 'PUT',
                        success: function (result) {
                            if (result.code == 401) {
                                location.href = "/login.html";
                                return;
                            }
                            if (result.code == 1) {
                                table.reload('category-table', {});
                                layer.close(index);
                            } else {
                                alert(result.msg)
                            }
                            layer.closeAll('loading');
                        }
                    })
                }, success: function (layero, index) {
                    form.render()
                    if (data != null) {
                        form.val("category-edit-form", data);
                        form.val("category-edit-form", {
                            "state": data.state + ""
                        });
                    }

                }
            });
        }

        form.on('submit(category-table)', function (data) {
            table.reload('category-table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: data.field
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    })
    ;
    //自定义函数
    $.fn.serializeJson = function () {
        var serializeObj = {};
        var array = this.serializeArray();
        $.each(array, function () {
            if (serializeObj[this.name] !== undefined) {
                if (!serializeObj[this.name].push) {
                    serializeObj[this.name] = [serializeObj[this.name]];
                }
                serializeObj[this.name].push(this.value || '');
            } else {
                serializeObj[this.name] = this.value || '';
            }
        });
        return serializeObj;
    };
</script>

<script type="text/html" id="category-edit-layer">
    <form id="category-edit-form" style="width: 80%" method="post" className="layui-form"
          lay-filter="category-edit-form">
        <input type="hidden" name="id">
        <div className="layui-form-item">
            <label className="layui-form-label">用户名称</label>
            <div className="layui-input-block">
                <input type="text" name="user" required lay-verify="required" placeholder="用户名称"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>
        <div className="layui-form-item">
            <label className="layui-form-label">参数</label>
            <div className="layui-input-block">
                <input type="text" name="params" required lay-verify="required" placeholder="参数"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>

        <div className="layui-form-item">
            <label className="layui-form-label">用户操作</label>
            <div className="layui-input-block">
                <input type="text" name="caozuo" required lay-verify="required" placeholder="用户操作"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>

        <div className="layui-form-item">
            <label className="layui-form-label">时间</label>
            <div className="layui-input-block">
                <input type="text" name="date" required lay-verify="required" placeholder="时间"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>
        <div className="layui-form-item">
            <label className="layui-form-label">状态</label>
            <div className="layui-input-block">
                <input type="radio" name="msg" title="success" value="1"/>
                <input type="radio" name="msg" title="无效" value="0"/>
            </div>
        </div>


    </form>
</script>


<script type="text/html" id="category-add-layer">
    <form id="category-add-form" style="width: 80%" method="put" className="layui-form" lay-filter="category-edit-form">

        <div className="layui-form-item">
            <label className="layui-form-label">用户名称</label>
            <div className="layui-input-block">
                <input type="text" name="user" required lay-verify="required" placeholder="用户名称"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>
        <div className="layui-form-item">
            <label className="layui-form-label">参数</label>
            <div className="layui-input-block">
                <input type="text" name="params" required lay-verify="required" placeholder="参数"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>
        <div className="layui-form-item">
            <label className="layui-form-label">用户操作</label>
            <div className="layui-input-block">
                <input type="text" name="caozuo" required lay-verify="required" placeholder="用户操作"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>

        <div className="layui-form-item">
            <label className="layui-form-label">时间</label>
            <div className="layui-input-block">
                <input type="text" name="date" required lay-verify="required" placeholder="时间"
                       autoComplete="off"
                       className="layui-input">
            </div>
        </div>
        <div className="layui-form-item">
            <label className="layui-form-label">状态</label>
            <div className="layui-input-block">
                <input type="radio" name="msg" title="success" value="1"/>
                <input type="radio" name="msg" title="无效" value="0"/>
            </div>
        </div>


    </form>
</script>